﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;

namespace DrawingScreen.Objects
{
    public class CCircle: CDrawingObject
    {
        public int x = 0;
        public int y = 0;

        public int r = 1;

        public Pen pen = new Pen(Color.Black);

        public override void Draw(System.Drawing.Graphics g)
        {
            g.DrawEllipse(pen, x - r, y - r, r + r, r + r);
        }

        public static CCircle CreateDrawingObject(XmlElement eleObject)
        {
            try
            {
                XmlElement eleCircle = (XmlElement)eleObject.FirstChild;

                CCircle c = new CCircle();
                c.x = Int32.Parse(eleCircle.Attributes["x"].Value);
                c.y = Int32.Parse(eleCircle.Attributes["y"].Value);
                c.r = Int32.Parse(eleCircle.Attributes["r"].Value);

                Pen pen = Global.CreatePenFromXmlElement((XmlElement)eleCircle.FirstChild);

                if (pen != null)
                {
                    c.pen = pen;
                }

                return c;
            
            }
            catch
            {
                return null;
            }
        }

        public override XmlNode convertToXmlNode(XmlDocument doc)
        {
            try
            {
                XmlElement eleObject = doc.CreateElement("OBJECT");

                XmlAttribute attObjectID = doc.CreateAttribute("id");
                attObjectID.Value = CDrawingObject.CIRCLE_ID.ToString();
                eleObject.Attributes.Append(attObjectID);

                //////////////////////////////////////////////////////////////
                XmlElement eleCircle = doc.CreateElement("CIRCLE");

                XmlAttribute attCircleX = doc.CreateAttribute("x");
                attCircleX.Value = x.ToString();
                eleCircle.Attributes.Append(attCircleX);

                XmlAttribute attCircleY = doc.CreateAttribute("y");
                attCircleY.Value = y.ToString();
                eleCircle.Attributes.Append(attCircleY);

                XmlAttribute attCircleR = doc.CreateAttribute("r");
                attCircleR.Value = r.ToString();
                eleCircle.Attributes.Append(attCircleR);

                XmlElement elePen = Global.CreateXmlElementFromPen(doc, pen);
                eleCircle.AppendChild(elePen);

                //////////////////////////////////////////////////////////////

                eleObject.AppendChild(eleCircle);

                return eleObject;
            }
            catch
            {
                return null;
            }
        }
    }
}
